﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/User.Master" AutoEventWireup="true" CodeBehind="ApplicationMain.aspx.cs" Inherits="CRMWeb.Pages.ApplicationMain" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:content id="Content2" contentplaceholderid="ContentPlaceHolder1" runat="server">
    <table id="dg" style="" autorowheight="true"
        pagination="true" data-options="pageSize:30,toolbar:'#tbApplicationList'"
        rownumbers="true" fitcolumns="true" singleselect="true" remotesort="true">
        <thead>
            <tr>
                <th rowspan="2" field="app_id" sortable="true" width="15px">App NO.</th>
                <th colspan="2">Applicant Name </th>
                <th rowspan="2" field="DesiredAmount" width="25px" data-options="formatter:ReplaceNumberWithCommas" sortable="true">Desired Amount</th>
                <th rowspan="2" field="app_result" width="50px" sortable="true">Last Result</th>
                <th rowspan="2" field="cdate" width="35px" sortable="true">Create Date</th>
                <th rowspan="2" field="edate" width="35px" sortable="true">Edit Date</th>
                <th rowspan="2" field="applicationprofileid" data-options="formatter:formatApplicationProfile">Profile</th>
            </tr>
            <tr>
                <th field="applicant1" width="30px" sortable="true">First Applicant</th>
                <th field="SecondApplicant" width="30px" sortable="true">Second Applicant</th>
            </tr>
        </thead>
    </table>
    <div id="tbApplicationList" style="padding: 5px; height: auto">
        Field: 
            <select id="cmbSearchField" class="easyui-combobox" panelheight="auto" style="width: 150px" data-options="onSelect:setControl">
                <option value="Email">Email</option>
                <option value="a.app_id">Application No</option>
                <option value="FirstName">First Name</option>
                <option value="SurName">Surname</option>
                <option value="REPLACE(Mobile,' ','')">Mobile</option>
                <option value="REPLACE(HomePhone,' ','')">Home Phone</option>
                <option value="State">State</option>
                <option value="DateBirth">Birthdate</option>
                <option value="a.app_result">Application Status</option>
            </select>
        Operator: 
            <select id="cmbSearchOperator" class="easyui-combobox" panelheight="auto" style="width: 100px">
                <option value="  like  ">Start With</option>
                <option value=" like ">Contains</option>
                <option value="=">Equals</option>
            </select>
        Value:
        <input id="txtSearchValue" class="easyui-validatebox" />
        &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
        <span id="spnAppStatus" style="display: none">
            <input id="cmbStatus" class="easyui-combobox" style="width: 250px" data-options=" 
                    valueField: 'ResultDescription',  
                    textField: 'ResultDescription',  
                    url: '/WebServices/ApplicationInfo.ashx?method=LastResultData'" />
        </span>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-search" onclick="quickSearch(' ')"></a>
    </div>
    <div id="divNotFoundMsg" style="display: none; width: 100%; height: 30px; border: solid; font-family: Cambria; font-size: medium; border-color: mistyrose; color: red; vertical-align: middle; text-align: center">
        No any data return
    </div>
    <script type="text/javascript">
        var ascSort = true;

        function setControl() {
            if ($('#cmbSearchField').combobox('getValue') == "a.app_result") {
                $('#spnAppStatus').show();
                $('#cmbStatus').combobox('setText', '<-- Select -->');
                $('#txtSearchValue').hide();
            }
            else {
                $('#spnAppStatus').hide();
                $('#txtSearchValue').show();
            }
        }
        function ReplaceNumberWithCommas(yourNumber) {
            if (yourNumber != null) {
                //Seperates the components of the number
                var components = yourNumber.toString().split(".");
                //Comma-fies the first part
                components[0] = components[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                //Combines the two sections
                return "$" + components.join(".");
            }
        }
        function formatApplicationProfile(value, row) {
            //var applicationId = row.app_id;
            if (row.PurposeSummary == "SACC")
                return "<a href='/Pages/Application/ApplicationDetailSACC.aspx?app_id=" + row.app_id + "&type=" + row.PurposeSummary + "' target='_blank' style='cursor:pointer'>View</a>";
            else
                return "<a href='/Pages/Application/ApplicationDetail.aspx?app_id=" + row.app_id + "&type=" + row.PurposeSummary + "' target='_blank' style='cursor:pointer'>View</a>";
        }
        $(document).ready(function () {
            document.title = "CRM  - Application List";
            var days = getQuerystring('days', '');
            var advSearchtext = getQuerystring('searchtext', '');
            if (advSearchtext == -1) {
                setCookie('LastSearchApp', '');
                advSearchtext = '';
            }
            else if (advSearchtext.length == 0)
                advSearchtext = getCookie('LastSearchApp');
            else
                setCookie('LastSearchApp', advSearchtext);

            if (days == 30)
                document.getElementById('rdb30days').checked = true;
            else if (days == 90)
                document.getElementById('rdb90days').checked = true;
            else if (days == 'All' || days.length == 0)
                document.getElementById('rdbAll').checked = true;

            $('#txtSearchValue').val('');
            $('#dg').datagrid({
                url: '/WebServices/ApplicationSelect.ashx?searchtext=' + advSearchtext + '&days=' + days,
                title: 'All Applications' + setPanelText(),
                onDblClickRow: function (index, rowdata) {
                    if (rowdata.PurposeSummary == "SACC")
                        window.location.href = "/Pages/Application/ApplicationDetailSACC.aspx?app_id=" + rowdata.app_id + "&type=" + rowdata.PurposeSummary;
                    else
                        window.location.href = "/Pages/Application/ApplicationDetail.aspx?app_id=" + rowdata.app_id + "&type=" + rowdata.PurposeSummary;
                },
                onSelect: function (rowIndex, rowData) {
                    document.onkeydown = function chekKey(e) {
                        e = e || window.event;
                        if (e.keyCode == '38') // up arrow
                        {
                            if (rowIndex == 0)
                                $('#dg').datagrid('selectRow', 29);
                            else
                                $('#dg').datagrid('selectRow', rowIndex - 1);
                        }
                        else if (e.keyCode == '40') // down arrow
                        {
                            if (rowIndex == 29)
                                $('#dg').datagrid('selectRow', 0);
                            else
                                $('#dg').datagrid('selectRow', rowIndex + 1);
                        }
                    }

                    document.onkeypress = function chekKey(e) {
                        e = e || window.event;
                        if (e.keyCode == '13') // Enter Key

                            if (rowData.PurposeSummary == "SACC")

                                window.location.href = "/Pages/Application/ApplicationDetailSACC.aspx?app_id=" + rowData.app_id + "&type=" + rowData.PurposeSummary;

                            else
                                window.location.href = "/Pages/Application/ApplicationDetail.aspx?app_id=" + rowData.app_id + "&type=" + rowData.PurposeSummary;

                    }
                },
                onSortColumn: function (sort, order) {
                    if (sort == "cdate")
                        sort = "a.CreatedDate";
                    else if (sort == "edate")
                        sort = "a.EditDate";

                    quickSearch(sort + '  ' + order);
                },
                onLoadSuccess: function (data) {
                    if (data.rows.length == 1) {
                        $('#divNotFoundMsg').hide();

                        if (data.rows[0].PurposeSummary == "SACC")
                            window.location.href = "/Pages/Application/ApplicationDetailSACC.aspx?app_id=" + data.rows[0].app_id + "&type=" + data.rows[0].PurposeSummary;
                        else
                            window.location.href = "/Pages/Application/ApplicationDetail.aspx?app_id=" + data.rows[0].app_id + "&type=" + data.rows[0].PurposeSummary;
                    }
                    else if (data.rows.length == 0) {
                        $('#divNotFoundMsg').show();
                    }
                    else {
                        $('#divNotFoundMsg').hide();
                    }
                }
            });
            setHeight();
            fnLeftNavSelected('A9');
            $('#cmbSearchField').combobox('textbox').bind('click', function () {
                $('#cmbSearchField').combobox('showPanel');
            });
            $('#cmbSearchOperator').combobox('textbox').bind('click', function () {
                $('#cmbSearchOperator').combobox('showPanel');
            });
            $('#cmbStatus').combobox('textbox').bind('click', function () {
                $('#cmbStatus').combobox('showPanel');
            });
        });

        document.onkeydown = function chekKey(e) {
            e = e || window.event;
            if (e.keyCode == '38') // up arrow
            {
                $('#dg').datagrid('selectRow', 29);
            }
            else if (e.keyCode == '40') // down arrow
            {
                $('#dg').datagrid('selectRow', 0);
            }
        }


        var isString_re = /^[a-zA-Z]*$/;
        function isString(s) {
            var s = String(s).search(isString_re) != -1
            if (s == true) {
                return 0;
            }
        }

        var isWhole_re = /^(?=.*\d)(?:[\d ]+)$/;
        function isNumber(s) {
            var s = String(s).search(isWhole_re) != -1
            if (s == true) {
                return 1;
            }
        }

        function isOnlyNumber(value) {
            var reg = new RegExp('^[0-9]+$');
            return reg.test(value);
        }

        var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
        function isEmail(s) {
            var s = String(s).search(isEmail_re) != -1;
            if (s == true) {
                return 2;
            }
        }

        var isAlphaNumeric_re = /^[a-zA-Z0-9]*$/;
        function isAlphaNumeric(s) {
            var s = String(s).search(isAlphaNumeric_re) != -1;
            if (s == true) {
                return 3;
            }
        }

        function setPanelText() {
            var str;
            var cookieText = getCookie('LastSearchApp');
            var ismobile = cookieText.replace(/%20/g, '');
            if ((cookieText == '') && ($('#txtSearchValue').val() == ''))
                str = '';
            else if ($('#txtSearchValue').val() != '' && cookieText == '')
                str = '( ' + $('#cmbSearchField').combobox('getText') + ' ' + $('#cmbSearchOperator').combobox('getText') + ' ' + $('#txtSearchValue').val() + ')';
            else if ($('#txtSearchValue').val() == '' && cookieText != '') {
                if (isOnlyNumber(ismobile) == true)
                    str = '(Mobile= ' + cookieText + ')';
                else if (isEmail(cookieText) == 2)
                    str = '(Email= ' + cookieText + ')';
                else if (isString(cookieText) == 0)
                    str = '(SurName= ' + cookieText + ')';
                else if (isAlphaNumeric(cookieText) == 3)
                    str = '(Loan Number= ' + cookieText + ')';
                else
                    str = '(FullName= ' + cookieText + ')';
            }
            else if ((cookieText != '') && ($('#txtSearchValue').val() != '')) {
                if (isOnlyNumber(ismobile) == true)
                    str = '(Mobile= ' + cookieText;
                else if (isEmail(cookieText) == 2)
                    str = '(Email= ' + cookieText;
                else if (isString(cookieText) == 0)
                    str = '(SurName= ' + cookieText;
                else if (isAlphaNumeric(cookieText) == 3)
                    str = '(Loan Number= ' + cookieText;
                else
                    str = '(FullName= ' + cookieText;
                str += ', ' + $('#cmbSearchField').combobox('getText') + ' ' + $('#cmbSearchOperator').combobox('getText') + ' ' + $('#txtSearchValue').val() + ')';
            }
            str = str.replace(/%20/g, ' ');
            return str;
        }


        function quickSearch(sortText) {

            var cmbSearchField = $('#cmbSearchField').combobox('getValue');
            var cmbSearchOperator = $('#cmbSearchOperator').combobox('getValue');
            var txtSearchValue = '';
            var strcondition = ' and ';
            if (cmbSearchField == "a.app_result")
                txtSearchValue = $('#cmbStatus').combobox('getValue');
            else
                txtSearchValue = $('#txtSearchValue').val().trim().replace(/\s/g, '');

            if (txtSearchValue.length >= 1) {
                if (cmbSearchField == 'Email') {
                    var strcondition = ' and (';
                    if (cmbSearchOperator == ' like ' || cmbSearchOperator == '  like  ') {
                        if (cmbSearchOperator == '  like  ')
                            //strat with                           
                            strcondition += cmbSearchField + cmbSearchOperator + "\'" + txtSearchValue + "%\'" + ' OR secondEmailAddress ' + cmbSearchOperator + '\'' + txtSearchValue + '%\'';
                        else
                            //Contains
                            strcondition += cmbSearchField + cmbSearchOperator + "\'%_" + txtSearchValue + "%\'" + ' OR secondEmailAddress ' + cmbSearchOperator + '\'%' + txtSearchValue + '%\'';
                    }
                    else // equals
                        strcondition += cmbSearchField + cmbSearchOperator + "\'" + txtSearchValue + "\'" + " OR secondEmailAddress " + cmbSearchOperator + "\'" + txtSearchValue + "\'";
                    strcondition += ')';
                }
                else {
                    var strcondition = ' and ';
                    if (cmbSearchOperator == ' like ' || cmbSearchOperator == '  like  ') {
                        if (cmbSearchOperator == '  like  ')
                            strcondition += cmbSearchField + cmbSearchOperator + "\'" + txtSearchValue + "%\'";
                        else
                            strcondition += cmbSearchField + cmbSearchOperator + "\'%_" + txtSearchValue + "%\'";
                    }
                    else
                        strcondition += cmbSearchField + cmbSearchOperator + "\'" + txtSearchValue + "\'";
                }
            }
            else
                strcondition = '';

            var days = getQuerystring('days', '');

            $('#dg').datagrid({
                title: 'All Applications ' + setPanelText(),
                url: '/WebServices/ApplicationSelect.ashx?searchtext=' + getCookie('LastSearchApp') + '&conditiontext=' + strcondition + '&sortCondition=' + sortText + '&days=' + days
            });
        }

        fnLeftNavSelected('A8');
    </script>

</asp:content>
